package  
{
	import flash.display.BitmapData;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import sourceproxy.ResourceProxy;
	import style.TextStyle;
	/**
	 * ...
	 * @author ...
	 */
	public class MMButton extends MMInteractive 
	{
		static public const TEXTALIGN_LEFT:String = "left";
		static public const TEXTALIGN_RIGHT:String = "right";
		static public const TEXTALIGN_CENTER:String = "center";
		
		
		protected var _Bg:MMImage;
		protected var _textAlign:String;
		protected var _label:MMLabel;
		protected var _text:String;
		
		private var _toggle:Boolean;
		
		private var _statusCount:uint;
		private var _currentStatus:uint;//1正常，2鼠标over，3鼠标按下，4禁用状态
		private var _statusBitmap:Array;
		
		public var onButtonDown:Function;
		
		
		public function MMButton(statusCount:uint=4) 
		{
			_statusCount = statusCount;
			_textAlign = TEXTALIGN_CENTER;
			mouseChildren = false;
			show();
		}
		
		override public function onInit():void
		{
			super.onInit();
			_Bg = new MMImage();
			_Bg.show();
			addChild(_Bg);
			
			
			_label = new MMLabel();
			_label.show();
			addChild(_label);
			initEvent();
		}
		
		protected function initEvent():void
		{
			_currentStatus = 1;
			
			onMouseOver = mouseOverHandler;
			onMouseOut = mouseOutHandler;
			onMouseDown = mouseDownHandler;
			onMouseUp = mouseUpHandler;
		}
		
		private function mouseUpHandler(e:MouseEvent):void 
		{
			_currentStatus = 2;
			updateStatus();
			
		}
		
		private function mouseDownHandler(e:MouseEvent):void 
		{
			_currentStatus = 3;
			updateStatus();
		}
		
		private function mouseOutHandler(e:MouseEvent):void 
		{
			_currentStatus = 1;
			updateStatus();
		}
		
		private function mouseOverHandler(e:MouseEvent):void 
		{
			_currentStatus = 2;
			updateStatus();
		}
		
		override public function onHide():void
		{
			super.onHide();
		}
		
		override public function onShow():void
		{
			super.onShow();
			update();
		}
		
		override public function updateData():void
		{
			super.updateData();
			updateStatus();
			if (_text && _label.text != _text)
			{
				_label.text = _text;
				
			}
			this.updateTextAlign();
		}
		
		protected function updateTextAlign():void
		{
			
			var layerX:Number;
			if (_textAlign ==TEXTALIGN_LEFT)
			{
				layerX = 0;
			}else if (_textAlign == TEXTALIGN_RIGHT)
			{
				layerX = width -_label.width;
			}else
			{
				layerX = (width - _label.width) / 2;
			}
			_label.place(layerX, (height - _label.height) / 2);
		}
		
		
		
		
		private function updateStatus():void
		{
			if (_statusBitmap == null) return;
			if (_statusBitmap.length >= _currentStatus)
			{
				_Bg.bitmapData = _statusBitmap[_currentStatus-1];
			}
		}
		
		private function cutBackgroupImage(bitmapData:BitmapData):void
		{
			_statusBitmap = new Array();
			var tempBitmapData:BitmapData;
			var matrix:Matrix = new Matrix();
			var width:uint = bitmapData.width / _statusCount;//按钮长度
			for (var i:uint = 0; i < _statusCount; i++)
			{
				matrix.tx = i * -width;
				tempBitmapData = new BitmapData(width, bitmapData.height);
				tempBitmapData.copyPixels(bitmapData, new Rectangle(i * width, 0, width, bitmapData.height), new Point());
				_statusBitmap.push(tempBitmapData);
			}
			
			update();
		}
		
		public function set label(text:String):void
		{
			_text = text;
			update();
		}
		public function get label():String
		{
			return _text;
		}
		
		public function set textStyle(textStyle:TextStyle):void
		{
			_label.textStyle = textStyle;
			update();
		}
		
		public function set backgroupImageUrl(url:String):void
		{
			var bitmapData:BitmapData = ResourceProxy.getBitmapData(url);
			if (!bitmapData)
			{
				ResourceProxy.instance().push(url);
				ResourceProxy.instance().load(
					function() :void
					{
						cutBackgroupImage(ResourceProxy.getBitmapData(url));
					}
				);
			}else
			{
				cutBackgroupImage(bitmapData);
			}
		}
		public function set backgroupImage(bitmapData:BitmapData):void
		{
			cutBackgroupImage(bitmapData);
		}
		
		
		override public function set enable(v:Boolean):void
		{
			super.enable = v;
			if (v)
				_currentStatus = 1;
			else
				_currentStatus = 4;
			updateStatus();
		}
		
		public function get textAlign():String { return _textAlign; }
		
		public function set textAlign(value:String):void 
		{
			_textAlign = value;
			update();
		}
	}

}